<?php

namespace common\helpers;


use common\models\base\TreatmentProject;
use common\models\member\Member;
use common\models\patient\PatientType;
use common\models\rbac\Action;
use common\models\rbac\Menu;
use common\models\rbac\Role;
use common\models\shop\Shop;
use common\models\system\SystemConfig;
use Yii;

class RedisHelper
{
    // 项目命名空间
    const KEY_TITLE = 'kq_spokesman';

    /**
     * 菜单key
     * @param int $menuId
     * @return string
     */
    private static function getMenuKey(int $menuId): string
    {
        return self::KEY_TITLE.":menu:{$menuId}";
    }

    /**
     * 获取菜单
     * @param int $menuId
     * @return Menu|null
     */
    public static function getMenu(int $menuId): ?Menu
    {
        $key = self::getMenuKey($menuId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setMenu($data = Menu::findOne($menuId));
        return $data;
    }

    /**
     * 设置菜单
     * @param Menu|null $menu
     * @return void
     */
    public static function setMenu(?Menu $menu): void
    {
        if (empty($menu)) {
            return;
        }
        $key = self::getMenuKey($menu->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($menu));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 动作key
     * @param int $actionId
     * @return string
     */
    private static function getActionKey(int $actionId): string
    {
        return self::KEY_TITLE.":action:{$actionId}";
    }

    /**
     * 获取动作
     * @param int $actionId
     * @return Action|null
     */
    public static function getAction(int $actionId): ?Action
    {
        $key = self::getActionKey($actionId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setAction($data = Action::findOne($actionId));
        return $data;
    }

    /**
     * 设置动作
     * @param Action|null $action
     * @return void
     */
    public static function setAction(?Action $action): void
    {
        if (empty($action)) {
            return;
        }
        $key = self::getActionKey($action->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($action));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 动作key
     * @param int $actionId
     * @return string
     */
    private static function getRoleKey(int $actionId): string
    {
        return self::KEY_TITLE.":role:{$actionId}";
    }

    /**
     * 获取动作
     * @param int $roleId
     * @return Role|null
     */
    public static function getRole(int $roleId): ?Role
    {
        $key = self::getRoleKey($roleId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setRole($data = Role::findOne($roleId));
        return $data;
    }

    /**
     * 设置动作
     * @param Role|null $role
     * @return void
     */
    public static function setRole(?Role $role): void
    {
        if (empty($role)) {
            return;
        }
        $key = self::getRoleKey($role->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($role));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除角色
     * @param int|array $ids
     * @return void
     */
    public static function delRole($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getRoleKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 人员key
     * @param int $id
     * @return string
     */
    private static function getMemberKey(int $id): string
    {
        return self::KEY_TITLE.":member:{$id}";
    }

    /**
     * 获取人员
     * @param int $id
     * @return Member|null
     */
    public static function getMember(int $id): ?Member
    {
        $key = self::getMemberKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setMember($data = Member::findOne($id));
        return $data;
    }

    /**
     * 设置人员
     * @param Member|null $action
     * @return void
     */
    public static function setMember(?Member $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getMemberKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除人员
     * @param int|array $ids
     * @return void
     */
    public static function delMember($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getMemberKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 系统配置key
     * @param string $name
     * @return string
     */
    private static function getSystemConfigKey(string $name): string
    {
        return self::KEY_TITLE.":system_config:{$name}";
    }

    /**
     * 获取系统配置
     * @param string $name
     * @return SystemConfig|null
     */
    public static function getSystemConfig(string $name): ?SystemConfig
    {
        $key = self::getSystemConfigKey($name);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setSystemConfig($data = SystemConfig::findOne(['name' => $name]));
        return $data;
    }

    /**
     * 设置系统配置
     * @param SystemConfig|null $model
     * @return void
     */
    public static function setSystemConfig(?SystemConfig $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getSystemConfigKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除系统配置
     * @param int|array $ids
     * @return void
     */
    public static function delSystemConfig($ids): void
    {
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getSystemConfigKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 患者类型key
     * @param int $id
     * @return string
     */
    private static function getPatientTypeKey(int $id): string
    {
        return self::KEY_TITLE.":patient_type:{$id}";
    }

    /**
     * 获取患者类型
     * @param int $id
     * @return PatientType|null
     */
    public static function getPatientType(int $id): ?PatientType
    {
        $key = self::getPatientTypeKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setPatientType($data = PatientType::findOne($id));
        return $data;
    }

    /**
     * 设置患者类型
     * @param PatientType|null $model
     * @return void
     */
    public static function setPatientType(?PatientType $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getPatientTypeKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除患者类型
     * @param int|array $ids
     * @return void
     */
    public static function delPatientType($ids): void
    {
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getPatientTypeKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 项目key
     * @param int $id
     * @return string
     */
    private static function getTreatmentProjectKey(int $id): string
    {
        return self::KEY_TITLE.":treatment_project:{$id}";
    }

    /**
     * 获取项目
     * @param int $id
     * @return TreatmentProject|null
     */
    public static function getTreatmentProject(int $id): ?TreatmentProject
    {
        $key = self::getTreatmentProjectKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setTreatmentProject($data = TreatmentProject::findOne($id));
        return $data;
    }

    /**
     * 设置项目
     * @param TreatmentProject|null $model
     * @return void
     */
    public static function setTreatmentProject(?TreatmentProject $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getTreatmentProjectKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除项目
     * @param int|array $ids
     * @return void
     */
    public static function delTreatmentProject($ids): void
    {
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getTreatmentProjectKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 门店key
     * @param int $id
     * @return string
     */
    private static function getShopKey(int $id): string
    {
        return self::KEY_TITLE.":shop:{$id}";
    }

    /**
     * 获取门店
     * @param int $id
     * @return Shop|null
     */
    public static function getShop(int $id): ?Shop
    {
        $key = self::getShopKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setShop($data = Shop::findOne($id));
        return $data;
    }

    /**
     * 设置门店
     * @param Shop|null $model
     * @return void
     */
    public static function setShop(?Shop $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getShopKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除门店
     * @param int|array $ids
     * @return void
     */
    public static function delShop($ids): void
    {
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getShopKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }
}
